

package com.maoshi.shop.delivery.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maoshi.shop.bean.model.Order;
import com.maoshi.shop.bean.model.OrderItem;
import com.maoshi.shop.bean.model.UserAddrOrder;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.delivery.comment.model.DeliveryOrder;
import com.maoshi.shop.delivery.comment.param.DeliveryOrderItemParam;
import com.maoshi.shop.delivery.comment.param.DvyOrderParam;
import com.maoshi.shop.delivery.comment.service.DeliveryOrderService;
import com.maoshi.shop.security.multishop.util.SecurityUtils;
import com.maoshi.shop.service.OrderItemService;
import com.maoshi.shop.service.OrderService;
import com.maoshi.shop.service.UserAddrOrderService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lhd on 2020/05/15.
 */
@Controller
@RequestMapping("/order/delivery")
public class OrderDeliveryController {

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private UserAddrOrderService userAddrOrderService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private DeliveryOrderService deliveryOrderService;




    /**
     * 订单项待发货数量查询
     */
    @GetMapping("/getOrderItemUnDelivery/{orderNumber}")
    public ResponseEntity<Order> getOrderItemUnDelivery(@PathVariable("orderNumber") String orderNumber) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        Order order = orderService.getOrderByOrderNumberAndShopId(orderNumber,shopId,true);
        UserAddrOrder userAddrOrder = userAddrOrderService.getById(order.getAddrOrderId());
        order.setUserAddrOrder(userAddrOrder);
        List<OrderItem> orderItems = orderItemService.list(new LambdaQueryWrapper<OrderItem>()
                        .eq(OrderItem::getOrderNumber, orderNumber).eq(OrderItem::getShopId, shopId));
        if(CollectionUtils.isEmpty(orderItems)){
            throw new MaoshiShopBindException("订单不存在！");
        }
        //查询已发货数量
//        List<DeliveryOrderItem> deliveryOrderItems = deliveryOrderItemService.listItemNums(orderItems);
//        if(CollectionUtils.isNotEmpty(deliveryOrderItems)){
//            Map<Long, Integer> deliveryMap = deliveryOrderItems.stream().collect(
//                    Collectors.toMap(DeliveryOrderItem::getOrderItemId, DeliveryOrderItem::getProdCount));
//            if(deliveryMap.containsKey(orderItem.getOrderItemId())){
//                orderItem.setProdCount(orderItem.getProdCount() - deliveryMap.get(orderItem.getOrderItemId()));
//            }
//        }
        for (OrderItem orderItem : orderItems) {
            //根据状态查询可发货数量
            orderItem.setChangeNum(orderItem.getStatus() == -1 ?orderItem.getProdCount():orderItem.getStatus());
            orderItem.setProdCount(orderItem.getChangeNum());
        }
        order.setOrderItems(orderItems);
        return ResponseEntity.ok(order);
    }

    /**
     * 订单项分包裹发货
     */
    @PutMapping("/orderItemsDelivery")
    public ResponseEntity<Void> orderItemsDelivery(@RequestBody DeliveryOrderItemParam deliveryOrderParam) {
        deliveryOrderService.saveDeliveriesInfo(deliveryOrderParam);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询订单物流包裹信息
     */
    @PutMapping("/getOrderDeliveries/{orderNumber}")
    public ResponseEntity<List<DeliveryOrder>> getOrderDeliveries(@PathVariable("orderNumber") String orderNumber) {
        List<DeliveryOrder> deliveryOrders = deliveryOrderService.list(
                new LambdaQueryWrapper<DeliveryOrder>().eq(DeliveryOrder::getOrderNumber, orderNumber).eq(DeliveryOrder::getStatus,1));
        return ResponseEntity.ok(deliveryOrders);
    }


    /**
     * 修改订单物流包裹信息
     */
    @PutMapping("/updateOrderDeliveries")
    public ResponseEntity<Void> updateOrderDeliveries(DvyOrderParam dvyOrderParam) {
        List<DeliveryOrder> deliveryOrders = dvyOrderParam.getDeliveryOrders();
        deliveryOrderService.updateOrderDeliveries(deliveryOrders);
        return ResponseEntity.ok().build();
    }

}
